package com.gzdh.tang.config.youxin;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.Map;

public class YouXinRequest {

    public Map<String, Object> getBalanceByPhone(String phone) {
        String appId = YouXinPayUtil.getAppId();
        String url = YouXinPayUtil.getUrl() + "/api/wallet/getBalanceByPhone";
        String accessToken = YouXinPayUtil.getAccessToken();
        String key = "app_id=" + appId + "&phone_number=" + phone + "&access_token=" + accessToken;
        String keySign = DigestUtils.md5Hex(key);
        Map<String, Object> resMap = null;
        try {
            CloseableHttpClient httpClient = HttpClients.createDefault();
            //创建请求对象
            HttpPost httpPost = new HttpPost(url);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("app_id", appId);
            jsonObject.put("phone_number", phone);
            jsonObject.put("key_sign", keySign);
            StringEntity entity = new StringEntity(jsonObject.toString());
            //指定请求编码方式
            entity.setContentEncoding("utf-8");
            //数据格式
            entity.setContentType("application/json");
            httpPost.setEntity(entity);
            //发送请求
            CloseableHttpResponse response = httpClient.execute(httpPost);
            //解析返回结果
            int statusCode = response.getStatusLine().getStatusCode();
            System.out.println("响应码为：" + statusCode);

            HttpEntity httpEntity = response.getEntity();
            String body = EntityUtils.toString(httpEntity);
            System.out.println("响应数据为：" + body);
            resMap = JSON.parseObject(JSON.parseObject(body).get("data").toString());
            //关闭资源
            response.close();
            httpClient.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return resMap;
    }

    public Map<String, Object> orderPay(String orderNo, String orderAmount, String dinnerTime, String phone) {
        String appId = YouXinPayUtil.getAppId();
        String url = YouXinPayUtil.getUrl() + "/api/wallet/orderPay";
        String accessToken = YouXinPayUtil.getAccessToken();
        String key = "app_id=" + appId
                + "&dinner_time=" + dinnerTime
                + "&order_amount=" + orderAmount
                + "&order_no=" + orderNo
                + "&phone_number=" + phone
                + "&access_token=" + accessToken;
        String keySign = DigestUtils.md5Hex(key);
        Map<String, Object> resMap = null;
        try {
            CloseableHttpClient httpClient = HttpClients.createDefault();
            //创建请求对象
            HttpPost httpPost = new HttpPost(url);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("app_id", appId);
            jsonObject.put("phone_number", phone);
            jsonObject.put("dinner_time", dinnerTime);
            jsonObject.put("order_no", orderNo);
            jsonObject.put("order_amount", orderAmount);
            jsonObject.put("key_sign", keySign);
            StringEntity entity = new StringEntity(jsonObject.toString());
            //指定请求编码方式
            entity.setContentEncoding("utf-8");
            //数据格式
            entity.setContentType("application/json");
            httpPost.setEntity(entity);
            //发送请求
            CloseableHttpResponse response = httpClient.execute(httpPost);
            //解析返回结果
            int statusCode = response.getStatusLine().getStatusCode();
            System.out.println("响应码为：" + statusCode);

            HttpEntity httpEntity = response.getEntity();
            String body = EntityUtils.toString(httpEntity);
            System.out.println("响应数据为：" + body);
            resMap = JSON.parseObject(body);
            //关闭资源
            response.close();
            httpClient.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return resMap;
    }

    public Map<String, Object> orderRefund(String orderNo, String refundAmount, String phone, String reason) {
        String appId = YouXinPayUtil.getAppId();
        String url = YouXinPayUtil.getUrl() + "/api/wallet/orderRefund";
        String accessToken = YouXinPayUtil.getAccessToken();
        String key = "app_id=" + appId
                + "&order_no=" + orderNo
                + "&phone_number=" + phone
                + "&reason=" + reason
                + "&refund_amount=" + refundAmount
                + "&access_token=" + accessToken;
        String keySign = DigestUtils.md5Hex(key);
        Map<String, Object> resMap = null;
        try {
            CloseableHttpClient httpClient = HttpClients.createDefault();
            //创建请求对象
            HttpPost httpPost = new HttpPost(url);
            JSONObject jsonObject = new JSONObject(true);
            jsonObject.put("app_id", appId);
            jsonObject.put("order_no", orderNo);
            jsonObject.put("phone_number", phone);
            jsonObject.put("reason", reason);
            jsonObject.put("refund_amount", refundAmount);
            jsonObject.put("key_sign", keySign);
            StringEntity entity = new StringEntity(jsonObject.toString());
            //指定请求编码方式
            entity.setContentEncoding("utf-8");
            //数据格式
            entity.setContentType("application/json");
            httpPost.setEntity(entity);
            //发送请求
            CloseableHttpResponse response = httpClient.execute(httpPost);
            //解析返回结果
            int statusCode = response.getStatusLine().getStatusCode();
            System.out.println("响应码为：" + statusCode);

            HttpEntity httpEntity = response.getEntity();
            String body = EntityUtils.toString(httpEntity);
            System.out.println("响应数据为：" + body);
            resMap = JSON.parseObject(body);
            //关闭资源
            response.close();
            httpClient.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return resMap;
    }
}
